package handle

import (
	"context"
	desc "gitee.com/jinmingzhi/cdce-service/api/protos/desc/v1alpha"
	"gitee.com/jinmingzhi/cdce-service/internal/service"
	"github.com/golang/protobuf/ptypes/empty"
)

type Desc struct {
	userSvc service.User
}

func NewDesc (userSvc service.User) *Desc {
	return &Desc{userSvc:userSvc}
}


func (d *Desc) Info(ctx context.Context, _ *empty.Empty) (userInfo *desc.InfoResp, err error) {
	uid, err := GetUid(ctx)
	if err != nil {
		return nil, handlerErr(err)
	}
	userInfo = &desc.InfoResp{}

	User, UserDetail, err := d.userSvc.FindUser(ctx, uid)
	if err != nil {
		return nil, handlerErr(err)
	}
	userInfo.User = modelToPbUser(User)
	userInfo.UserDetail = modelToPbUserDetail(UserDetail)
	return userInfo, nil
}

func (d *Desc) PasswdSet(ctx context.Context, req *desc.PasswdSetReq) (*empty.Empty, error) {
	uid, err := GetUid(ctx)
	if err != nil {
		return nil, handlerErr(err)
	}

	err = d.userSvc.SetPasswd(ctx, uid, req.OldPasswd, req.NewPasswd)
	if err != nil {
		return nil, err
	}
	return &empty.Empty{}, nil
}

func (d *Desc) RegInfoSet(ctx context.Context, req *desc.RegInfoSetReq) (*empty.Empty, error) {
	return &empty.Empty{}, nil
}
